package cn.like.backEnd.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.like.backEnd.biz.service.AuthResourceService;
import cn.like.backEnd.biz.service.AuthRoleService;
import cn.like.backEnd.biz.service.AuthUserService;
import cn.like.backEnd.rocket.test.TestSender;
import cn.like.base.support.exception.code.ExceptionCode;
import cn.like.base.support.result.Rest;
import cn.like.common.dto.SignInRequestDto;
import cn.like.entity.AuthUser;
import cn.like.stream.helper.MessageDelayLevel;
import cn.like.stream.helper.RocketMessageCreator;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author like 980650920@qq.com
 * @date 2021/7/10 19:12
 */
@RestController
public class AuthUserController {

    // inject
    private final AuthUserService authUserService;
    private final AuthResourceService authResourceService;
    private final AuthRoleService authRoleService;
    private final TestSender testSender;

    public AuthUserController(AuthUserService authUserService, AuthResourceService authResourceService, AuthRoleService authRoleService, TestSender testSender) {
        this.authUserService = authUserService;
        this.authResourceService = authResourceService;
        this.authRoleService = authRoleService;
        this.testSender = testSender;
    }

    @GetMapping("/admin/send")
    public Rest<?> send(@RequestParam("message") String message) {
        final SendResult send = testSender.send(RocketMessageCreator.of(testSender.topic(),message + "-" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN)).delay(MessageDelayLevel.five_second).create());
        return Rest.of(send);
    }

    @GetMapping("/admin/sendBatch")
    public Rest<?> sendBatch(@RequestParam("message") String message) {
        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            messages.add(RocketMessageCreator.of(testSender.topic(),message + "-" + i + "-" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN)).create());
            try {
                TimeUnit.MICROSECONDS.sleep(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        testSender.sendBatch(messages);
        return Rest.of();
    }

    @GetMapping("/admin/findByAccount")
    public Rest<AuthUser> findByAccount(@RequestParam("account") String account) {
        return Rest.of(authUserService.findByAccount(account));
    }

    @GetMapping("/admin/findResourceCodeByUserId")
    public Rest<List<String>> findResourceCodeByUserId(@RequestParam("userId") Object userId) {
        return Rest.of(authResourceService.findResourceCodeByUserId(userId));
    }

    @GetMapping("/admin/findUserRoleByUserId/")
    public Rest<List<String>> findUserRoleByUserId(@RequestParam("userId") Object userId) {
        return Rest.of(authRoleService.findUserRoleByUserId(userId));
    }

    @PostMapping("/admin/createUser")
    public Rest<AuthUser> createAuthUser(@RequestBody SignInRequestDto signInRequestDto) {
        final AuthUser user = authUserService.findByAccount(signInRequestDto.getAccount());
        if (!Objects.isNull(user)) {
            return Rest.failed(ExceptionCode.USER_IS_EXIST);
        }

        return Rest.of(authUserService.createAuthUser(signInRequestDto));
    }
}
